package com.example.zhangxin.myhuatuob2b.fragment.home.store;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zhangxin.myhuatuob2b.MyApplication;
import com.example.zhangxin.myhuatuob2b.R;
import com.example.zhangxin.myhuatuob2b.utils.PreferenceUtils;
import com.example.zhangxin.myhuatuob2b.utils.URL;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserNameLoginFragment extends Fragment implements View.OnClickListener {


    private EditText loginNameID;
    private LinearLayout ly_edit_name;
    private EditText loginPassWordID;
    private LinearLayout ly_edit_password;
    private RelativeLayout ly_check_box;
    private LinearLayout submitID;
    private TextView tv_rem_pw;
    private TextView registeredID;

    public UserNameLoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_name_login, container, false);

        initView(view);
        return view;
    }

    private void initView(View view) {
        loginNameID = (EditText) view.findViewById(R.id.loginNameID);
        ly_edit_name = (LinearLayout) view.findViewById(R.id.ly_edit_name);
        loginPassWordID = (EditText) view.findViewById(R.id.loginPassWordID);
        ly_edit_password = (LinearLayout) view.findViewById(R.id.ly_edit_password);
        ly_check_box = (RelativeLayout) view.findViewById(R.id.ly_check_box);
        submitID = (LinearLayout) view.findViewById(R.id.submitID);
        tv_rem_pw = (TextView) view.findViewById(R.id.tv_rem_pw);
        registeredID = (TextView) view.findViewById(R.id.registeredID);

        submitID.setOnClickListener(this);
        tv_rem_pw.setOnClickListener(this);
        registeredID.setOnClickListener(this);

    }

    private void submit() {
        // validate
        String loginName = loginNameID.getText().toString().trim();
        if (TextUtils.isEmpty(loginName)) {
            Toast.makeText(getContext(), "loginNameIDString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String loginPassword = loginPassWordID.getText().toString().trim();
        if (TextUtils.isEmpty(loginPassword)) {
            Toast.makeText(getContext(), "loginPassWordIDString不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        infoLoginCheck(loginName, loginPassword);//用户登录

    }

    private void infoLoginCheck(String loginName, String loginPassword) {

        String url = URL.URL_LOGIN;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("username", loginName);
        params.addBodyParameter("password", loginPassword);
        params.addBodyParameter("client", "android");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println(result);
                try {
                    JSONObject object = new JSONObject(result);
                    if(!object.isNull("datas")){
                        JSONObject obj = object.getJSONObject("datas");
                        if(obj.getBoolean("isOK")){
                            String key = obj.optString("key");
                            String member_role = obj.optString("member_role");
                            String msg = obj.getString("msg");
                            String userid = obj.optString("userid");
                            String username = obj.optString("username");

                            PreferenceUtils.setString(MyApplication.getAppcation(),"key",key);
                            PreferenceUtils.setString(MyApplication.getAppcation(),"userid",userid);
                            PreferenceUtils.setString(MyApplication.getAppcation(),"username",username);
                            PreferenceUtils.setString(MyApplication.getAppcation(),"member_role",member_role);

                            getActivity().finish();
                        }else{
                            Toast.makeText(getActivity(),"登录失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submitID:

                submit();

                break;
            case R.id.tv_rem_pw:

                break;
            case R.id.registeredID:

                break;
        }
    }
}
